package com.quanyan.stadium.biz.redis.impl;

import com.quanyan.redisCluster.utils.RedisConstants;
import com.quanyan.redisCluster.utils.RedisKey;
import com.quanyan.stadium.biz.redis.GradeIntegralCacheDao;
import com.quanyan.stadium.component.cache.MyRedisTemplate;
import com.quanyan.stadium.component.cache.StadiumRedisConstants;
import com.quanyan.stadium.entity.TbGameGradeIntegral;
import com.quanyan.stadium.entity.TbGameGradeIntegralExample;
import com.quanyan.stadium.entity.TbUserIntegral;
import com.quanyan.stadium.entity.TbUserIntegralExample;
import com.quanyan.stadium.mapper.TbGameGradeIntegralMapper;
import com.quanyan.stadium.mapper.TbUserIntegralMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author dongshenglin
 * @date 2016/7/26  19:18
 */
@Service
public class GradeIntegralCacheDaoImpl implements GradeIntegralCacheDao {

    private final static Logger logger = LoggerFactory.getLogger(GradeIntegralCacheDaoImpl.class);

    @Autowired
    private TbGameGradeIntegralMapper tbGameGradeIntegralMapper;

    @Autowired
    private TbUserIntegralMapper tbUserIntegralMapper;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    private final static int EXPIRE_TIME = 24*60*60+10;

    @Override
    public void putGradeIntegral() {
        TbGameGradeIntegralExample tbGameGradeIntegralExample = new TbGameGradeIntegralExample();
        List<TbGameGradeIntegral> tbGameGradeIntegralList = tbGameGradeIntegralMapper.selectByExample(tbGameGradeIntegralExample);
        Integer loadLine = 0;
        logger.info("redis开始加载评级积分表");
        if (null != tbGameGradeIntegralList && tbGameGradeIntegralList.size() >0){
            for (TbGameGradeIntegral gameGradeIntegral : tbGameGradeIntegralList){
                /**
                 * key值是：level+gameType
                 */
                String key = StadiumRedisConstants.getGradeIntegralKey(gameGradeIntegral.getLevel().intValue(), gameGradeIntegral.getGameType());
                RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY, key);
                myRedisTemplate.set(redisKey,gameGradeIntegral);
                myRedisTemplate.expire(redisKey,EXPIRE_TIME);
                loadLine++;
                logger.info("加载：" + gameGradeIntegral.toString());
            }
            logger.info("redis加载评级积分表完成");
            logger.info("==一共加载了"+loadLine+"行==");
        }else{
            logger.error("redis加载评级积分表出错：info:从数据库中表tb_game_grade_integral获取不到数据！");
        }
    }

    @Override
    public void clearUserGradeInfo(){
        logger.info("-----开始刷新用户评级积分信息！----");
        TbUserIntegralExample example = new TbUserIntegralExample();
        List<TbUserIntegral> userIntegralList = tbUserIntegralMapper.selectByExample(example);
        int i = 0;
        if(null != userIntegralList && userIntegralList.size() > 0){
            for(TbUserIntegral userIntegral : userIntegralList){
                String key = StadiumRedisConstants.getUserGradeKey(userIntegral.getUserId(),userIntegral.getGameCategoryId());
                RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY,key);
                myRedisTemplate.delteKey(redisKey);
                logger.info("用户uid:"+userIntegral.getUserId()+",栏目id:"+userIntegral.getGameCategoryId()+" 已刷新");
                i++;
            }
        }
        logger.info("用户评级信息刷新成功！共刷新了 "+i+"条记录！");
    }

    @Override
    public void clearUserGradeInfo(Integer uid, Integer categoryId) {
       if(null != uid && null != categoryId ) {
           TbUserIntegralExample example = new TbUserIntegralExample();
           TbUserIntegralExample.Criteria criteria = example.createCriteria();
           criteria.andUserIdEqualTo(uid);
           criteria.andGameCategoryIdEqualTo(categoryId);
           List<TbUserIntegral> userIntegralList = tbUserIntegralMapper.selectByExample(example);
           int i = 0;
           if(null != userIntegralList && userIntegralList.size() == 1){
               String key = StadiumRedisConstants.getUserGradeKey(uid,categoryId);
               RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY,key);
               myRedisTemplate.delteKey(redisKey);
               logger.info("用户uid:" + uid + ",栏目id:" + categoryId + " 已刷新");
               i++;
           }
           logger.info("用户评级信息刷新成功！共刷新了 " + i + "条记录！");
       }
    }

    @Override
    public void clearGradeIntegral(){
        logger.info("--开始更新评级表缓存--");
        this.putGradeIntegral();
        logger.info("--开始刷新用户评级缓存--");
        this.clearUserGradeInfo();
        logger.info("--评级表、用户评级信息缓存更新完成--");
    }

    @Override
    public boolean isUserGradeFlag(Integer uid, Integer categoryId) {
        if(null != uid && null!=categoryId){
            Date date = new Date();
            String key = StadiumRedisConstants.isSettingUserGrade(uid,date,categoryId);
            RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY,key);

            long flag = myRedisTemplate.setnx(redisKey);
            if (flag != 0) {
                this.setUserGradeFlag(uid, categoryId);
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public void setUserGradeFlag(Integer uid,Integer categoryId) {
        if(null != uid){
            Date date = new Date();
            String key = StadiumRedisConstants.isSettingUserGrade(uid,date,categoryId);
            RedisKey redisKey = new RedisKey(RedisConstants.STADIUM_FAMILY_KEY,key);
            myRedisTemplate.set(redisKey,uid);
            myRedisTemplate.setWithExpireTime(redisKey,uid,EXPIRE_TIME);
        }
    }
}
